home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Communication / NewsBase / Source / INewsRc.m < prev    next >
Text File  |  1993-01-12  |  10KB  |  322 lines

  1. /*$Copyright:
  2.  * Copyright (C) 1992.5.22. Recruit Co.,Ltd. 
  3.  * Institute for Supercomputing Research
  4.  * All rights reserved.
  5.  * NewsBase  by ISR, Kazuto MIYAI, Gary ARAKAKI, Katsunori SUZUKI, Kok-meng Lue
  6.  *
  7.  * You may freely copy, distribute and reuse the code in this program under 
  8.  * following conditions.
  9.  * - to include this notice in the source code, if it is to be distributed 
  10.  *   with source code.
  11.  * - to add the file named "COPYING" within the code, which shall include 
  12.  *   GNU GENERAL PUBLIC LICENSE(*).
  13.  * - to display an acknowledgement in binary code as follows: "This product
  14.  *   includes software developed by Recruit Co.,Ltd., ISR."
  15.  * - to display a notice which shall state that the users may freely copy,
  16.  *   distribute and reuse the code in this program under GNU GENERAL PUBLIC
  17.  *   LICENSE(*)
  18.  * - to indicate the way to access the copy of GNU GENERAL PUBLIC LICENSE(*)
  19.  *
  20.  *   (*)GNU GENERAL PUBLIC LICENSE is stored in the file named COPYING
  21.  * 
  22.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  23.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  24.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  25. $*/
  26. /* INewsRc */
  27.  
  28. #import "INewsRc.h"
  29. #import "InfoD.h"
  30. #import "ITreeNodeD.h"
  31. #import "data_types.h"
  32. #import "errdebug.h"
  33.  
  34. #import <appkit/Application.h>
  35. #import <appkit/Panel.h>
  36. #import <objc/NXStringTable.h>
  37. #import <streams/streams.h>
  38. #import <stdio.h>
  39. #import <string.h>
  40. #import <libc.h>
  41. #import <objc/zone.h>
  42. #import <mach/mach.h>
  43.  
  44. #import "Localization.h"
  45.  
  46. #define LoStr(key)      doLocalString(NULL,key,NULL)
  47.  
  48. @implementation INewsRc
  49.  
  50. - initFile:(const char *)krcfile
  51. {
  52.     char    buf[MAXPATHLEN*2];
  53.  
  54.     [super init];
  55.     
  56.     if (!(strcmp(krcfile,""))) {         /* default is $HOME/.newsrc */
  57.     strcpy (iNewsRcFileName, NXHomeDirectory());
  58.     strcat (iNewsRcFileName, "/.newsrc");
  59.     } else {
  60.     strcpy (iNewsRcFileName, krcfile);
  61.     }
  62.  
  63.     DBG(10,fprintf(stderr,"-- iNewsRcFileName = %s is set\n",
  64.                             iNewsRcFileName));
  65.  
  66.     /* check command line length for sprintf */
  67.     if ((strlen(iNewsRcFileName)*2 + 6) > (MAXPATHLEN*2)) {
  68.     fprintf (stderr, "ERROR:INewsRc: file name is over buf\n");
  69.     return NULL;
  70.     }
  71.     sprintf (buf, "cp %s %s~", iNewsRcFileName, iNewsRcFileName);
  72.     if (system (buf)) {
  73.     /* 'cp' command failed */
  74.     if(strlen(iNewsRcFileName)>MAXPATHLEN) {
  75.         fprintf (stderr, "ERROR:INewsRc: file name is over buf\n");
  76.         return NULL;
  77.     }
  78.     /* because user specifield not existing file */
  79.     sprintf(buf,"%s is not found, NewsBase will make it",iNewsRcFileName);
  80.     if (NXRunAlertPanel(LoStr("NewsBase"),LoStr(buf),
  81.         LoStr("OK"),LoStr("Cancel"),NULL) == 0) {
  82.         /* Cancel is clicked */
  83.         return NULL;
  84.     }
  85.     return self;
  86.     } else {
  87.         return self;
  88.     }
  89. }
  90.  
  91. - readRcfile
  92. {
  93.     /* read .newsrc file and make string table */
  94.     char    *groupname;
  95.     char    *group_line;
  96.     char    ch;
  97.     char    line_buf[LINE_BUF_MAXLEN];
  98.     char    *line_ptr;
  99.  
  100.     /* initialize string table */
  101.     istringTableZone = NXCreateZone (vm_page_size, vm_page_size, YES);
  102.     istringTable = [[NXStringTable allocFromZone:istringTableZone] init];
  103.     
  104.     if((iFileStream=NXMapFile(iNewsRcFileName, NX_READWRITE))!=NULL) {
  105.     /* if newsrc file exist */
  106.     NXSeek (iFileStream, 0, NX_FROMSTART);
  107.  
  108.     while (ch=NXGetc(iFileStream) != EOF) {
  109.         NXUngetc(iFileStream);
  110.         /* get one line */
  111.         NXScanf(iFileStream, "%4087[^\n]", line_buf); 
  112.         if (strlen(line_buf) >= (LINE_BUF_MAXLEN-1)) {
  113.         NXRunAlertPanel(LoStr("NewsBase"),
  114.         LoStr("newsrc file : line is too long"),LoStr("OK"),NULL,NULL);
  115.         }
  116.         NXGetc(iFileStream);                /* get \n */
  117.  
  118.         group_line = NXCopyStringBufferFromZone(line_buf,istringTableZone);
  119.         /* replace ':' or '!' to '\0' */
  120.         line_ptr = line_buf;
  121.         if ((line_ptr=strchr(line_buf, ':')) != NULL) {
  122.         ;
  123.         } else if ((line_ptr=strchr(line_buf, '!')) != NULL) {
  124.         ;
  125.         } else {
  126.         /* line is empty so go to next line */
  127.         line_buf[0]='\0';
  128.         continue;
  129.         }
  130.         *line_ptr = '\0';
  131.         /* copy newsgroup name into groupname */
  132.         groupname = NXCopyStringBufferFromZone(line_buf,istringTableZone);
  133.         [istringTable insertKey:(char *)groupname 
  134.                             value:(char *)group_line];
  135.         line_buf[0]='\0';
  136.     }
  137.     NXCloseMemory (iFileStream, NX_FREEBUFFER);
  138.     return self;
  139.     } else {
  140.     fprintf(stderr, "INewsRc:warning: can't map .newsrc file\n");
  141.     return NULL;
  142.     }
  143. }
  144.  
  145. - (void)saveToRcfileFrom:knewsGroupTreeRoot
  146. {
  147.     /* save groupInfo into .newsrc file with istringTable */
  148.     NXTreeState        state;
  149.     id        node;
  150.     id        groupInfo;
  151.     NXStream    *memoryStream;
  152.     NXHashState    hashState;
  153.     const void    *groupname;
  154.     void    *group_line;
  155.     char    buf[128];
  156.  
  157.     [knewsGroupTreeRoot initState:&state];
  158.     while ((node=[knewsGroupTreeRoot nextState:&state]) != nil) {
  159.     if ([node isMemberOf:[ITreeNodeD class]] == YES) {
  160.         if ((groupInfo=[node dataForKey:GROUPINFO]) != nil) {
  161.             [self _writeToTableGroupInfo:groupInfo];
  162.         }
  163.     }
  164.     }
  165.        
  166.     memoryStream = NXOpenMemory (NULL, 0, NX_WRITEONLY);
  167.     hashState = [istringTable initState];
  168.     while ([istringTable nextState:&hashState 
  169.                         key:&groupname value:&group_line]) {
  170.     NXPrintf(memoryStream, "%s\n", (char *)group_line);
  171.     }
  172.     if (NXSaveToFile (memoryStream, iNewsRcFileName) != 0 ) {
  173.     sprintf(buf,"error occured writing data to file \"%.90s\"",
  174.                             iNewsRcFileName);
  175.     NXRunAlertPanel(LoStr("NewsBase"),LoStr(buf),LoStr("OK"),NULL,NULL);
  176.     }
  177.     NXCloseMemory(memoryStream, NX_FREEBUFFER);
  178. }
  179.  
  180. - (NewsStat)isSubscribe:(char *)groupname
  181. {
  182.     const char     *group_line;
  183.     
  184.     if ((group_line=[istringTable valueForStringKey:groupname]) == NULL) {
  185.     /* groupname is not in table, so this news group is new one */
  186.     return NotExist;
  187.     } else {
  188.     if (strchr(group_line,':') != NULL) {
  189.         /* "comp.sys.next: ..." -> subscribe */ 
  190.         return Subscribe;
  191.     } else {
  192.         /* "comp.sys.next! ..." -> unsubscribe */ 
  193.         return UnSubscribe;
  194.     }
  195.     }
  196. }
  197.  
  198. - setGroupInfo:kgroupInfo
  199. {
  200.     [self _readRcfileAndSetGroupInfo:kgroupInfo];
  201.     return self;
  202. }
  203.  
  204. - (void)_writeToTableGroupInfo:kgroupInfo
  205. {
  206.     char    *groupname, *groupnameKey;
  207.     char    *art_num_set;
  208.     char    *group_line, *old_line;
  209.     
  210.     groupname = (char *)[kgroupInfo infoForKey:GROUPNAME];
  211.     art_num_set = (char *)[kgroupInfo infoForKey:ART_NUM_SET];
  212.     if ([istringTable valueForStringKey:groupname] == NULL) {
  213.     /* key is not in string table, so allocate memory for key */
  214.     groupnameKey = NXCopyStringBufferFromZone(groupname,istringTableZone);
  215.     } else {
  216.     groupnameKey = groupname;
  217.     }
  218.     
  219.     group_line = (char *)NXZoneMalloc(istringTableZone, 
  220.                     (strlen(groupnameKey)+strlen(art_num_set)+3));
  221.     if (!(strcmp([kgroupInfo infoForKey:SUBSCRIBE], "yes"))) {
  222.         sprintf (group_line, "%s: %s", groupname, art_num_set);
  223.     } else {
  224.         sprintf (group_line, "%s! %s", groupname, art_num_set);
  225.     }
  226.     /* insert group_line to string table */
  227.     /* if news group is not new one, and exist in table */
  228.     /* free the old group_line which has "groupname:...." */
  229.     if((old_line = [istringTable insertKey:groupnameKey value:group_line])
  230.                                         !=NULL){
  231.     NXZoneFree(istringTableZone, old_line);
  232.     }
  233.  
  234.     return;
  235. }
  236.  
  237. //- readGroupInfo:sender
  238. //{
  239. //    id        groupInfo;
  240.     
  241. //    if ((groupInfo=[sender dataForKey:GROUPINFO]) != nil) {
  242. //    [self _readRcfileAndSetGroupInfo:groupInfo];
  243. //    }
  244. //    return self;
  245. //}
  246.  
  247. - (void)_readRcfileAndSetGroupInfo:kgroupInfo
  248. {
  249.     char    *groupname, *iLineBuf_ptr, ch;
  250.     char    buf[256];
  251.     int        first;
  252.     
  253.     groupname = (char *)[kgroupInfo infoForKey:GROUPNAME];
  254.                         /* if group is exist in .newsrc */
  255.                     /* and it has ':' or ';' */
  256.     if (([self _getLineGroup:groupname]!='\0') && 
  257.           (strchr(iLineBuf, ':') || strchr(iLineBuf, '!'))) {
  258.     iLineBuf_ptr = iLineBuf;
  259.                         /* check subscription */
  260.     if (strchr(iLineBuf, '!')) {
  261.         [kgroupInfo addInfoString:"no" key:SUBSCRIBE];
  262.     } else {                
  263.         [kgroupInfo addInfoString:"yes" key:SUBSCRIBE];
  264.     }
  265.                                 
  266.     while ( (ch = *(++iLineBuf_ptr)) != ':' && ch != '!')
  267.         ;                /* move ptr. after ':' or '!' */
  268.     while ( (ch = *(++iLineBuf_ptr)) == ' ' )
  269.         ;
  270.                             /* skip space */
  271.     if (ch=='\0') {            /* if iLineBuf is "groupname: " */
  272.                         /* art_num_set is "0-(LAST-1)" */
  273.         first = (int)[kgroupInfo infoForKey:FIRST] - 1;
  274.         if (first < 0) {
  275.         first = 0;
  276.         }
  277.         sprintf (buf, "0-%d", first);
  278.         [kgroupInfo addInfoString:buf key:ART_NUM_SET];
  279.         return;
  280.     } else {
  281.         [kgroupInfo addInfoString:iLineBuf_ptr key:ART_NUM_SET];
  282.     }
  283.                 /* read xxx-xxx store xxx-xxx to groupInfo */
  284.     } else {
  285.                         /* if not group in .newsrc file */
  286.                     /* set initial value to groupInfo */
  287.                         /* art_num_set is "1- (FIRST-1)" */
  288.     first = (int)[kgroupInfo infoForKey:FIRST] - 1;
  289.     if (first < 0) {
  290.         first = 0;
  291.     }
  292.     sprintf (buf, "0-%d", first);
  293.     [kgroupInfo addInfoString:"yes" key:SUBSCRIBE];
  294.     [kgroupInfo addInfoString:buf key:ART_NUM_SET];
  295.  
  296.     /* sould be changed to display new added group to screen */
  297. //    fprintf (stderr, "WARNING: new group \"%s\" is added\n", groupname);
  298.     }
  299.     return;
  300. }
  301.  
  302. - (char *)_getLineGroup:(char *)kgroup
  303. {
  304.     const char    *group_line;
  305.  
  306.     iLineBuf[0] = '\0';
  307.     if ((group_line=[istringTable valueForStringKey:kgroup]) != NULL) {
  308.     /* len. of group_line < LINE_BUF_MAXLEN. if not, "-readRcfile:" */
  309.     /* must make alert panel */
  310.         strncpy (iLineBuf, group_line, sizeof(iLineBuf));
  311.     }
  312.     return iLineBuf;
  313. }
  314.  
  315. - free
  316. {
  317.     NXDestroyZone (istringTableZone);
  318.     return [super free];
  319. }
  320.  
  321. @end
  322.